package list;

/**
 * @ClassName: DoublyLinkList
 * @Author: wangyusong
 * @Date: 2020-10-08 21:34
 * @Desciprion: 双链表结构及其基本操作
 * @Version:
 */
public class DoublyLinkList<T> {
    /**基本结构*/
    private class DulNode{
        private T data;
        private DulNode prior,next;

        public DulNode(T data, DulNode prior, DulNode next) {
            this.data = data;
            this.prior = prior;
            this.next = next;
        }

        public DulNode(T data) {
            this.data = data;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public DulNode getPrior() {
            return prior;
        }

        public void setPrior(DulNode prior) {
            this.prior = prior;
        }

        public DulNode getNext() {
            return next;
        }

        public void setNext(DulNode next) {
            this.next = next;
        }
    }

    private DulNode head;
    /**插入*/
    public void add(T data){
        DulNode node = new DulNode(data);

        //插入头部
        if (this.head == null){
            head = node;
        }else if (this.head.next == null){
            //插入尾部
            this.head.next=node.prior;
        }else {
            //中间插入
            node.next = this.head.next;
            this.head.next.prior = node;
            node.prior=this.head;
            this.head.next=node;
        }
    }

    public void remove(T data){
          while (this.head.next != null){
              if (this.head.data == data){
                  DulNode curNode = this.head;
                  DulNode perNode = this.head.prior;
                  DulNode nextNode = this.head.next;

                  perNode.next = curNode.next;
                  nextNode.prior = curNode.prior;
              }
          }

    }
}
